﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;

namespace LongGi.TingkatKebocoranModule
{
   

    public static class ListToDataTableConverter
    {
        /// <summary>
        /// 将List集合转换为DataTable，只包含标有Description特性的属性
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="list">数据列表</param>
        /// <returns>DataTable</returns>
        public static DataTable ToDataTable<T>(this List<T> list)
        {
            DataTable dt = new DataTable();

            if (list == null || list.Count == 0)
                return dt;

            // 获取类型信息
            Type type = typeof(T);

            // 获取所有标有Description特性的属性
            var properties = type.GetProperties()
                .Where(p => p.GetCustomAttributes(typeof(DescriptionAttribute), false).Length > 0)
                .ToList();

            // 创建DataTable的列
            foreach (var prop in properties)
            {
                var descriptionAttr = (DescriptionAttribute)prop.GetCustomAttributes(typeof(DescriptionAttribute), false).First();
                string columnName = descriptionAttr.Description;

                // 处理可空类型
                Type columnType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;

                dt.Columns.Add(columnName, columnType);
            }

            // 填充数据行
            foreach (T item in list)
            {
                DataRow dr = dt.NewRow();

                foreach (var prop in properties)
                {
                    var descriptionAttr = (DescriptionAttribute)prop.GetCustomAttributes(typeof(DescriptionAttribute), false).First();
                    string columnName = descriptionAttr.Description;

                    object value = prop.GetValue(item, null);
                    dr[columnName] = value ?? DBNull.Value;
                }

                dt.Rows.Add(dr);
            }

            return dt;
        }

        /// <summary>
        /// 使用System.ComponentModel.DescriptionAttribute的版本
        /// </summary>
        public static DataTable ToDataTableWithSystemDescription<T>(this List<T> list)
        {
            DataTable dt = new DataTable();

            if (list == null || list.Count == 0)
                return dt;

            Type type = typeof(T);

            // 获取所有标有System.ComponentModel.DescriptionAttribute特性的属性
            var properties = type.GetProperties()
                .Where(p => p.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false).Length > 0)
                .ToList();

            // 创建DataTable的列
            foreach (var prop in properties)
            {
                var descriptionAttr = (System.ComponentModel.DescriptionAttribute)prop
                    .GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false).First();
                string columnName = descriptionAttr.Description;

                Type columnType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                dt.Columns.Add(columnName, columnType);
            }

            // 填充数据行
            foreach (T item in list)
            {
                DataRow dr = dt.NewRow();

                foreach (var prop in properties)
                {
                    var descriptionAttr = (System.ComponentModel.DescriptionAttribute)prop
                        .GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false).First();
                    string columnName = descriptionAttr.Description;

                    object value = prop.GetValue(item, null);
                    dr[columnName] = value ?? DBNull.Value;
                }

                dt.Rows.Add(dr);
            }

            return dt;
        }
    }
}
